/******************************************************************
 * Created by: DO HOANG HAI                                       
 * Date of Creation: 3/Jan/2008
 * Date of last modification: 23/2/2008
 * Status: Optimized and Finished
 *                                                                
 * Description: Provide method to access Buddy data in the database.
 ******************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Pbdms.BusinessLogic.DataController
{
    /// <summary>
    /// Provide means to obtain and manage data stored in the file buddy.xml
    /// </summary>
    public class BuddyController
    {
        /// <summary>
        /// create an instance of Buddy
        /// </summary>
        /// <returns></returns>
        public static BuddyController create()
        {
            return new BuddyController();
        }
        /// <summary>
        /// get the list of all buddies in the database
        /// </summary>
        /// <returns></returns>
        public ArrayList getListOfBuddies()
        {
            return DataAccess.DataProvider.BuddyProvider.create().getListOfBuddies();
        }
        /// <summary>
        /// update a buddy to database
        /// </summary>
        /// <param name="bdd"></param>
        public void updateBuddyToDB(Common.DataObject.Buddy bdd)
        {
            DataAccess.DataProvider.BuddyProvider.create().updateBuddyToDB(bdd);
        }
        /// <summary>
        /// append a new buddy to the database
        /// </summary>
        /// <param name="bdd"></param>
        public void appendBuddyToDB(Common.DataObject.Buddy bdd)
        {
            DataAccess.DataProvider.BuddyProvider.create().appendBuddyToDB(bdd);
        }
        public Common.DataObject.Buddy appendBuddyToDB(String name, String keyId)
        {
            //get next id
            int id = 0;
            System.Collections.ArrayList arlBuddies = BusinessLogic.DataController.BuddyController.create().getListOfBuddies();
            if (arlBuddies != null && arlBuddies.Count != 0)
            {
                id = ((Common.DataObject.Buddy)arlBuddies[arlBuddies.Count - 1]).Id + 1;
            }
            else
            {
                id = 1;
            }
            Common.DataObject.Buddy bdd = new Pbdms.Common.DataObject.Buddy(id, name, keyId);

            BusinessLogic.DataController.BuddyController.create().appendBuddyToDB(bdd);
            return bdd;
        }

        /// <summary>
        /// delete a buddy from the database
        /// </summary>
        /// <param name="bdd"></param>
        public void deleteBuddyFromDB(Common.DataObject.Buddy bdd)
        {
            DataAccess.DataProvider.BuddyProvider.create().deleteBuddyFromDB(bdd);
        }
        //return a buddy id based on name
        public string selectIDFromDB(string name)
        {
            string temp = DataAccess.DataProvider.BuddyProvider.create().selectIDFromDB(name);
            return temp;
        }

        //return a buddy id based on keyid
        public string selectIDBasedOnKeyIDFromDB(string keyid)
        {
            string temp = DataAccess.DataProvider.BuddyProvider.create().selectIDBasedOnKeyIDFromDB(keyid);
            return temp;
        }
        public Common.DataObject.Buddy selectABuddyBasedOnIDFromDB(string id)
        {
            Common.DataObject.Buddy buddy = DataAccess.DataProvider.BuddyProvider.create().selectABuddyBasedOnIDFromDB(id);

            return buddy;
        }
        public Common.DataObject.Buddy selectABuddyBasedOnKeyIDFromDB(string keyID)
        {
            Common.DataObject.Buddy buddy = DataAccess.DataProvider.BuddyProvider.create().selectABuddyBasedOnKeyIDFromDB(keyID);

            return buddy;
        }

    }
}
